package com.emr.common;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;


/**
 * 公共分页类v1.0.0 beta
 * @author Hearken
 * @date Jul 30, 2010 4:28:01 PM
 */
public class SpringPageBean {
	
	private boolean isOutPrint = false;// 是否输出监控信息
	private int pageSize = 0;
	private int pageNum = 0;
	private int totalNum = 0;
	private int totalPage = 0;
	private String querySql = null;
	private String countSql = null;
	private String orderBy = "";
	private String sqlCond = "";
	private List<?> resultList = null;
	private ArrayList<?> parList = null;
	
	public SpringPageBean(String[] sHqlArr, int[] iInfoArr, ArrayList<?> parList) {
		this.querySql = sHqlArr[0];
		this.countSql = sHqlArr[1];
		this.sqlCond = sHqlArr[2];
		this.orderBy = sHqlArr[3];
		this.pageNum = iInfoArr[0];
		this.parList = parList;
		if (pageNum <= 0) {
			pageNum = 1;
			iInfoArr[0] = 1;
		}
		this.pageSize = iInfoArr[1];
		if (pageSize <= 0) {
			pageSize = 20;
			iInfoArr[1] = 20;
		}
	}
	
	/**
	 * 执行查询
	 */
	public List<?> doInJdbcTemplate(JdbcTemplate jdbcTemplate) throws SQLException {
		Object[] parObj = null;
		if (isOutPrint) System.out.println("--------------查询监控 开始--------------");
		if (querySql == null || "".equals(querySql.trim())) {
			System.out.println("查询语句不能为空！");
			if (isOutPrint) System.out.println("--------------查询监控 结束--------------");
			return null;
		}
		if (countSql == null || "".equals(countSql.trim())) {
			countSql = null;
		}
		ArrayList<Object> setParList = new ArrayList<Object>();
		// 生成查询语句
		if (parList != null && parList.size() > 0) {
			parObj = new Object[parList.size()];// 条件
			for (int i = 0; i < parList.size(); i++) {
				Object[] condObj = (Object[]) parList.get(i);
				if (condObj == null || condObj.length < 1) {
					continue;
				}
				String sCond = (String) condObj[0];
				Object oPar = (Object) condObj[1];
				if (sCond == null || oPar == null || "".equals(oPar)) {
					if (isOutPrint) System.out.println("参数不足，不作条件！sCond=" + sCond);
					continue;
				}
				if ("%null%".equals(oPar) || "null%".equals(oPar) || "%null".equals(oPar) || "%%".equals(oPar)) {
					if (isOutPrint) System.out.println("参数不足，不作条件！sCond=" + sCond);
					continue;
				}
				querySql += sCond;
				if (countSql != null) {
					countSql += sCond;
				}
				setParList.add(oPar);
				// 设置条件
				parObj[i] = oPar;
				if (isOutPrint) System.out.println("par[" + i + "]=" + oPar);
			}
			// 加上自定义sql
			if (this.sqlCond != null && this.sqlCond.length() > 0) {
				querySql += this.sqlCond;
				if (countSql != null) {
					countSql += this.sqlCond;
				}
			}
			// 加上orderby
			if (this.orderBy != null && this.orderBy.length() > 0) {
				querySql += this.orderBy;
			}
		}
		setParList.clear();
		setParList = null;
		// 判断是否需要执行统计条数
		if (countSql != null) {
			// 执行总条数查询语句
			if (isOutPrint) System.out.println("总条数查询语句=" + countSql);
			totalNum = jdbcTemplate.queryForInt(countSql, parObj);
			if (totalNum <= 0) {
				totalNum = 0;
			}
			if (isOutPrint) System.out.println("总条数=" + totalNum);
			// 页码若超出范围，则指向最后一页
			if (Integer.MAX_VALUE == this.pageNum || this.pageNum > getLastPageNumber()) {
				this.pageNum = getLastPageNumber();
			}
		} else {
			totalNum = 1;// 如果不统计条数，就默认为1，让下面的判断通过
		}
		// 执行查询数据语句
		if (totalNum > 0) {
			if (isOutPrint) System.out.println("查询数据=" + querySql);
			String tempQuerySql = "SELECT * FROM ";
			tempQuerySql +="(";
			tempQuerySql +="SELECT A.*, ROWNUM RN ";
			tempQuerySql +="FROM ("+querySql+") A ";
			tempQuerySql +="WHERE ROWNUM <= " + getThisPageLastElementNumber();
			tempQuerySql +=")";
			tempQuerySql +="WHERE RN >= " + getThisPageFirstElementNumber();
			resultList = jdbcTemplate.queryForList(querySql, parObj);
		} else {
			if (isOutPrint) System.out.println("总条数为0，不用执行查询数据语句！");
		}
		if (isOutPrint) System.out.println("--------------查询监控 结束--------------");
		return resultList;
	}
	
	/**
	 * 判断是否第一页
	 * @return
	 */
	public boolean isFirstPage() {
		return getThisPageNum() == 1;
	}
	
	/**
	 * 判断是否最后一页
	 * @return
	 */
	public boolean isLastPage() {
		return getThisPageNum() >= getLastPageNumber();
	}
	
	/**
	 * 判断是否还有下一页
	 * @return
	 */
	public boolean hasNextPage() {
		return getLastPageNumber() > getThisPageNum();
	}
	
	/**
	 * 判断是否有上一页
	 * @return
	 */
	public boolean hasPreviousPage() {
		return getThisPageNum() > 1;
	}
	
	/**
	 * 获取最后一页页码
	 * @return
	 */
	public int getLastPageNumber() {
		return totalNum % this.pageSize == 0 ? totalNum / this.pageSize : totalNum / this.pageSize + 1;
	}
	
	/**
	 * 获取List类型数据
	 * @return List数据源
	 */
	public Object getThisPageElements() {
		return resultList;
	}
	
	/**
	 * 获取总条数
	 * @return
	 */
	public int getTotalNum() {
		return totalNum;
	}
	
	/**
	 * 获取某一页的第一个元素的号码
	 * @return
	 */
	public int getThisPageFirstElementNumber() {
		return (getThisPageNum() - 1) * getPageSize() + 1;
	}
	
	/**
	 * 获取某一页的最后一个元素的号码
	 * @return
	 */
	public int getThisPageLastElementNumber() {
		int fullPage = getThisPageFirstElementNumber() + getPageSize() - 1;
		return getTotalNum() < fullPage ? getTotalNum() : fullPage;
	}
	
	/**
	 * 获取下一页的号码
	 * @return
	 */
	public int getNextPageNumber() {
		return getThisPageNum() + 1;
	}
	
	/**
	 * 获取上一页的号码
	 * @return
	 */
	public int getPreviousPageNumber() {
		return getThisPageNum() - 1;
	}
	
	/**
	 * 获取每页的个数
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}
	
	/**
	 * 获取当前页码
	 * @return
	 */
	public int getThisPageNum() {
		return pageNum;
	}
	
	/**
	 * 获取总页数
	 * @return
	 */
	public int getTotalPage() {
		if (totalNum % pageSize == 0) {
			totalPage = totalNum / this.pageSize;
		} else {
			totalPage = totalNum / this.pageSize + 1;
		}
		return totalPage;
	}
}